package com.bicon.botu.mall.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.mall.base.entity.HomeIcon;
import com.bicon.botu.mall.base.service.IHomeIconService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页图标 前端控制器
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/homePage/homeIcon")
public class HomeIconController extends BaseController {
    @Autowired
    IHomeIconService iHomeIconService;

    /**
     * 新增图标
     *
     * @param homeIconsJson 图标对象集合
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam(value = "homeIconsJson") String homeIconsJson) {
        List<HomeIcon> homeIcons = JSONObject.parseArray(homeIconsJson, HomeIcon.class);
        Boolean flag = iHomeIconService.insert(homeIcons);
        if (flag) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, flag);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500, flag);
        }
    }

    /**
     * 逻辑删除图标
     *
     * @param idJson 图标id集合
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam("idJson") String idJson) {
        return iHomeIconService.delete(idJson);
    }

    /**
     * 修改图标
     *
     * @param id         图标id
     * @param grayIcon   灰色图标
     * @param brightIcon 高亮图标
     * @param iconFlag   图标开关:0.开，1.关
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("id") Integer id,
                             @RequestParam(value = "grayIcon", required = false) String grayIcon,
                             @RequestParam(value = "brightIcon", required = false) String brightIcon,
                             @RequestParam(value = "iconFlag", required = false) Integer iconFlag) {
        return iHomeIconService.update(id, grayIcon, brightIcon, iconFlag);
    }
    /**
     * 初始化所有图标到redis
     *
     * @return
     */
    @RequestMapping(value = "/initHomeIconToRedis", method = RequestMethod.POST)
    public BaseResult initHomeIconToRedis() {
        return iHomeIconService.initHomeIconToRedis();
    }

    /**
     * 获取首页图标
     *
     * @return
     */
    @RequestMapping(value = "/getHomeIcons", method = RequestMethod.GET)
    public BaseResult getHomeIcons() {
        List<HomeIcon> homeIcons = new HomeIcon().selectList(new EntityWrapper<HomeIcon>().eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200, homeIcons);
    }
}
